﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Xml.Linq;
using System.IO;
using EzDesk.Clients.Utility;
using EzDesk.Utility.Json;

namespace EzDesk.DevHelper.FeedTemplateTool
{
    public partial class Preview : Form
    {
        public Preview()
        {
            InitializeComponent();
        }

        private string generationFeedXml(string value)
        {
            throw new NotImplementedException();
        }

        private void Preview_Load(object sender, EventArgs e)
        {

        }

        public bool IsError
        {
            set
            {
                panel1.BackColor = value ? Color.Red : Color.Green;
            }
        }

        internal void SetFeed(XElement thisTemplate, string content, Dictionary<string, string> parameters)
        {
            string txtId = thisTemplate.Element("id").Value;
            string txtType = thisTemplate.Element("type").Value;
            string txtBody = thisTemplate.Element("body").Value;
            string txtTitle = thisTemplate.Element("title").Value;

            string replacedText = Helper.MergeDataToEzML(content, parameters);

            //txtEzml.Text = replacedText;
            // 生成FeedXml
            string feedXml = generationFeedXml(txtId, txtType, replacedText, parameters);

            FeedCollection feedCollection;
            try
            {
                feedCollection = FeedCollection.Create(feedXml);
            }
            catch (Exception ex)
            {
                IsError = true;
                return;
            }
            txtEzml.Text = feedCollection[0].TransformedData.ToString();
            

            string html;
            //  生成Web预览
            using (StreamReader sr = new StreamReader("ez-sample.htm"))
            {
                html = sr.ReadToEnd();

                if (txtType == "1")
                {
                    // notification
                    html = html.Replace("{notificationHtml}", txtEzml.Text);
                }
                if (txtType == "0")
                {
                    // update
                    html = html.Replace("{UpdateHtml}", txtEzml.Text);
                }
            }

            webBrowser1.DocumentText = html;
            IsError = false;
            
        }

        private string generationFeedXml(string txtId, string txtType, string feedContent, Dictionary<string, string> parameters)
        {
            XElement feedItems = new XElement("feeditems");

            XElement root = new XElement("root");
            root.Add(new XAttribute("appportalurl", "http://localhost:19145"));
            root.Add(feedItems);

            int count = 0;
            string feedDataStr = JsonHelper.ConvertToJson(parameters);
           
            XElement feedItem = new XElement("feeditem");
            feedItem.Add(new XAttribute("appname", "testAppName"));
            feedItem.Add(new XAttribute("guid", txtId));
            feedItem.Add(new XAttribute("createon", DateTime.Now.ToString()));
            feedItem.Add(new XAttribute("updateon", DateTime.Now.ToString()));
            feedItem.Add(new XAttribute("lastchangeon", DateTime.Now.ToString()));
            feedItem.Add(new XAttribute("isavailable", true));
            feedItem.Add(new XAttribute("isread", false));
            feedItem.Add(new XAttribute("ismark", false));
            feedItem.Add(new XAttribute("type", txtType));
            feedItem.Add(new XAttribute("iconurl", "default.png"));

            feedItem.Add(new XElement("data", new XCData(feedDataStr)));

            feedItem.Add(new XElement("content", new XCData(feedContent)));

            feedItems.Add(feedItem);
            count++;


            feedItems.Add(new XAttribute("count", count));

            return root.ToString(); 
        }
    }
}